package com.sx.micowell.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.example.mvvm.until.LogUtils;
import com.example.mvvm.until.ToastUtils;

import com.sx.micowell.framework.app.AppConstant;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * WXPayEntryActivity 微信支付回调
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, AppConstant.WX_APP_ID);
        try {
            api.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        try {
            api.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onReq(BaseReq req) {
        LogUtils.e("onReq: " + req);
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        LogUtils.e("onResp: " + resp + "//" + resp.errCode);
        switch (resp.getType()) {
            case ConstantsAPI.COMMAND_PAY_BY_WX:
                switch (resp.errCode) {
                    case BaseResp.ErrCode.ERR_OK:
                        ToastUtils.showShort("支付成功");
                        break;
                    case BaseResp.ErrCode.ERR_USER_CANCEL:
                        ToastUtils.showShort("取消支付");
                        break;
                    default:
                        ToastUtils.showShort("支付失败");
                        break;
                }
                //EventBus.getDefault().post(new OnWxEntryRespEvent(resp.getType(), resp.errCode));
                break;
        }
        finish();
    }
}
